import yfinance as yf
import pandas as pd

ticker = "601919.SS"
ohlcv = yf.download(ticker, period="6mo")

def donchian_channel(ohlc: pd.DataFrame, n: int = 20):
    df = ohlc.copy()
    df["rolling_max"] = df["Adj Close"].rolling(n).max()
    df["rolling_min"] = df["Adj Close"].rolling(n).min()
    df["midline"] = (df["rolling_max"] + df["rolling_min"]) / 2
    df = df.dropna()
    return df

dc = donchian_channel(ohlcv)
dc["above_mid"] = dc["Adj Close"] > dc["midline"]
dc["above_20"] = dc["Adj Close"] > dc["Adj Close"].rolling(window=20).mean()
head = dc.tail(10).round(2)[["Open", "High", "Low", "Close", "Adj Close", "midline", "above_mid", "above_20"]]

head.columns = ["开盘价", "最高价", "最低价", "收盘价", "调整后收盘价", "中线", "收盘价是否突破中线", "收盘价是否突破20日线"]
print(head)
fileName=ticker+".csv"
head.to_csv(fileName, encoding="utf-8-sig")